import { defineStore } from "pinia";
import TypeSum from "@/TypeSum/entry";
import request from '@/API/entry'

export const store = defineStore('sumStore', {
    state: (): TypeSum['Store']['DetailState'] => {
        return {
            itemData: {}
        }
    },
    getters: {
        categoryView(state) {
            return state.itemData && state.itemData.categoryView ? state.itemData.categoryView : ''
        },
        skuInfo(state) {
            return state.itemData && state.itemData.skuInfo ? state.itemData.skuInfo : ''
        },
        spuSaleAttrList(state) {
            return state.itemData && state.itemData.spuSaleAttrList ? state.itemData.spuSaleAttrList : ''
        },
    },
    actions: {
        async getDefault(skuId: number) {
            const result = await request.ApiRequest.reqDetailListData(skuId) as TypeSum['AxiosData']['AxiosData']
            if (result.code === 200) {
                this.itemData = result.data
                return true
            } else {
                Promise.reject(new Error('err in WLAN!'))
            }
        },
        setDefultImg(url: string) {
            this.itemData.skuInfo.skuDefaultImg = url
        },
        async geiPushMark(skuId: number, skuNum: number) {
            const result = await request.ApiRequest.reqPushMark(skuId, skuNum) as TypeSum['AxiosData']['AxiosData']
            if (result.code == 200) {
                return true
            } else {
                Promise.reject(new Error('出现错误了'))
            }
        }
    }
})